package com.open.capacity.common.controller;

import cn.hutool.http.HttpResponse;
import com.open.capacity.utils.file.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;

/**
 * 用于查看错误日志文件
 *
 * @author zifangsky
 * @date 2018/4/26
 * @since 1.0.0
 */
@RestController
public class LogController {

    private static final Logger log = LoggerFactory.getLogger(LogController.class);
    /**
     * 错误日志目录
     */
    @Value("${file.errlogpath:D:/tonghui/finance/logs/error.log}")
    private String errlogpath;

    @RequestMapping(value = "errlogs/download", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public void getServerInfos(HttpServletRequest request, HttpServletResponse response) {
        try {
            String filePath = errlogpath;

            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "error.log"));
            FileUtils.writeBytes(filePath, response.getOutputStream());

        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }
}
